package com.ww.springboot.boot.algorithm.leetcode1;

import java.util.*;

/**
 * 描述：
 * 给你一个数组 nums ，数组中只包含非负整数。定义 rev(x) 的值为将整数 x 各个数字位反转得到的结果。比方说 rev(123) = 321 ， rev(120) = 21 。我们称满足下面条件的下标对 (i, j) 是 好的 ：
 * <p>
 * 0 <= i < j < nums.length
 * nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])
 * 请你返回好下标对的数目。由于结果可能会很大，请将结果对 109 + 7 取余 后返回。
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode.cn/problems/count-nice-pairs-in-an-array
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 *
 * @author 🧑 ‍wanwei
 * @since 2023-01-17 16:40
 */
public class CW1814 {

    public static void main(String[] args) {
        int[] nums = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
        System.out.println(countNicePairs(nums));
    }


    /**
     * 暴力破解：
     * 将所有数字两两对比
     *
     * @param nums
     * @return
     */
    public static int countNicePairs(int[] nums) {
        int[] numsRev = new int[nums.length];
        boolean[] isRev = new boolean[nums.length];
        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            numsRev[i] = rev(nums[i], map);
            if (numsRev[i] == nums[i]) {
                isRev[i] = true;
            }
        }
        //可以加个缓存 相同的数字不用再计算
        int size = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (i == j) {
                    continue;
                }
                boolean iIsRev = isRev[i];
                boolean jIsRev = isRev[j];
                if(iIsRev && jIsRev){
                    size++;
                    continue;
                }else if((iIsRev && !jIsRev) || (!iIsRev && jIsRev)){
                    continue;
                }
                int iVar = nums[i];
                int jVar = nums[j];
                if (iVar == jVar) {
                    size++;
                    continue;
                }
                int iVarRev = numsRev[i];
                int jVarRev = numsRev[j];
                if (iVar + jVarRev == iVarRev + jVar) {
                    size++;
                }
            }
        }

        return size;
    }

    private static int rev(int num, Map<Integer, Integer> map) {
        int numCopy = num;
        Integer integer = map.get(num);
        if (integer != null) {
            return integer;
        }
        List<Integer> list = new ArrayList<>();

        while (num > 0) {
            list.add(num % 10);
            num = num / 10;
        }

        int total = 0;
        for (int i = 0; i < list.size(); i++) {
            int index = list.size() - 1 - i;
            Integer value = list.get(i);
            switch (index) {
                case 0:
                    total += value;
                    break;
                case 1:
                    total += value * 10;
                    break;
                case 2:
                    total += value * 100;
                    break;
                case 3:
                    total += value * 1000;
                    break;
                case 4:
                    total += value * 10000;
                    break;
                case 5:
                    total += value * 100000;
                    break;
                case 6:
                    total += value * 1000000;
                    break;
                case 7:
                    total += value * 10000000;
                    break;
                case 8:
                    total += value * 100000000;
                    break;
                case 9:
                    total += value * 1000000000;
                    break;
            }
        }
        map.put(num, total);
        map.put(total, numCopy);
        return total;
    }
}
